// 获取拼写的部分的字符串
function getContentStr(keyword, tableList) {
	console.log("获取匹配的拼写部分")

	// 两个大写字母中间有一个英文句号，并且这两个字母都必须是大写的,如果有，把句号替换成空格
	function replaceDotWithSpace(str) {
		var pattern = /[A-Z]\.[A-Z]/g; // 匹配两个大写字母中间有一个英文句号的模式
		var hasMatch = pattern.test(str);
		if (hasMatch) {
			str = str.replace(pattern, function(match) {
				return match.replace('.', ' ');
			});
		}
		return str;
	}

	//删除英文字符串中的所有英文单词，除了个别的指定的字符串
	function removeWordsExcept(str, keyword) {
		function getAlphabetArray() {
			var alphabet = 'abcdefghijklmnopqrstuvwxyz';
			var alphabetArray = alphabet.toUpperCase().split('');
			return alphabetArray;
		}

		function mergeArrays(array1, array2) {
			var mergedArray = array1.concat(array2);
			return mergedArray;
		}

		//首字母大写
		function capitalizeFirstLetter(str) {
			var words = str.split(' ');
			for (var i = 0; i < words.length; i++) {
				words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
			}
			var result = words.join(' ');
			return result;
		}

		//26个英文字母
		var letters = getAlphabetArray();
		var except = ["APP"];
		var exceptions = mergeArrays(letters, except);

		var pattern = new RegExp("\\b(?!" + exceptions.join("|") + ")\\w+\\b", "g"); // 匹配不在例外列表中的单词的正则表达式
		var modifiedStr = str.replace(pattern, '');

		var keyword2 = capitalizeFirstLetter(keyword);
		modifiedStr = modifiedStr.replace(new RegExp("\\b" + keyword + "\\b", "g"), ''); // 移除 "apple"
		modifiedStr = modifiedStr.replace(new RegExp("\\b" + keyword2 + "\\b", "g"), ''); // 移除 "Apple"

		return modifiedStr;
	}

	// 在字符串中获取首字体为大写的英文单词
	function fnFilter(sentence) {
		function getCapitalizedWords(str) {
			var pattern = /\b[A-Z][a-zA-Z]{1,}\b/g; // 匹配首字母为大写且至少两个字母的英文单词
			var result = str.match(pattern);
			return result;
		}

		// 示例用法
		// var sentence = "This is a Sample Sentence. Hello World A P P L E Apple!";
		var capitalizedWords = getCapitalizedWords(sentence);
		console.log(capitalizedWords); // 输出 ["This", "Sample", "Sentence", "Hello", "World"]

		var str = sentence;
		if (capitalizedWords) {
			for (var i = 0; i < capitalizedWords.length; i++) {
				var item = capitalizedWords[i];
				str = str.split(item).join('');
			}
		}

		return str;
	}

	//首字母大写
	function capitalizeFirstLetter(str) {
		var words = str.split(' ');
		for (var i = 0; i < words.length; i++) {
			words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
		}
		var result = words.join(' ');
		return result;
	}

	//去空格
	function removeSpaces(str) {
		var pattern = /\s/g; // 匹配所有空格
		var result = str.replace(pattern, '');
		return result;
	}

	function extractPatterns(str) {
		var pattern = /([A-Z]+\s?){2,}(?![A-Z])/g; // 匹配至少两个连续的大写字母和空格的模式，并排除连续的大写字母
		var matches = str.match(pattern);
		if (matches) {
			var filteredMatches = matches.filter(match => !/^[A-Z\s]\$/.test(match.trim()));
			return filteredMatches;
		} else {
			return [];
		}
	}

	//删除英文字符串中的单词，除了全大写的单词
	function removeWordsExceptUpperCase(str, keyword) {
		var pattern = /\b(?![A-Z]+\b)\w+\b/g;
		var modifiedStr = str.replace(pattern, '');
		return modifiedStr;
	}

	//TODO 组合字符串
	var answerArr = [];
	for (let i = 0; i < tableList.length; i++) {
		if (tableList[i].isAnswer == true) {
			var tempStr = tableList[i].content;
			let filteredSentence = tempStr.replace(/I'm|I'd/g, function(match) {
				match = match.replace("'", " ");
				return match.toUpperCase();
			});
			answerArr.unshift(filteredSentence + " and ");
		}
	}
	var answerStr = answerArr.join('');

	console.log('#################################################################')
	console.log('#################################################################')
	//js 将英文语句中的单个字母的单词转换为大写
	answerStr = answerStr.replace(/\b[a-z]\b/g, function(match) {
		return match.toUpperCase();
	});

	//TODO 两个大写字母中间有一个英文句号，并且这两个字母都必须是大写的,如果有，把句号替换成空格
	console.log("answerStr:开始", answerStr)
	answerStr = replaceDotWithSpace(answerStr);
	for (var i = 0; i < 100; i++) {
		answerStr = replaceDotWithSpace(answerStr);
	}
	console.log("answerStr:结束", answerStr)
	// var wordFirstBig = capitalizeFirstLetter(keyword)
	// answerStr = answerStr.split(wordFirstBig).join('');
	// console.log("answerStr-----", answerStr)
	// var answerStr = "Speech.Speech a.P.Oh.L.E.Apple.Just see you eattea.Doesala.Had quite.A P P L E Apple.";
	// answerStr = fnFilter(answerStr);//去除在字符串中获取首字体为大写的英文单词
	//TODO 删除英文字符串中的所有英文单词，除了个别的指定的字符串
	// answerStr = removeWordsExcept(answerStr, keyword);

	//TODO 删除英文字符串中的单词，除了全大写的单词
	answerStr = removeWordsExceptUpperCase(answerStr, keyword);
	console.log('fnFilter后的结果：', answerStr)

	//TODO 提炼匹配的部分
	var patterns = extractPatterns(answerStr);
	console.log('提炼匹配的部分', patterns)

	if (patterns === null) {
		return '';
	} else {
		var list = [];
		for (var i = 0; i < patterns.length; i++) {
			var item = patterns[i];
			item = removeSpaces(item);
			list.push(item)
		}
		console.log('提炼匹配的部分2', list)
		return list.join('');
	}
}

//付志鹏：对拼写的字符串进行状态判断：-1错 0等待 1正确
function getSpellStatus(answer, userAnswer) {
	answer = answer.toLowerCase().trim();
	userAnswer = userAnswer.toLowerCase().trim();
	console.log('[' + answer + ']：：：：：：：：：：：：：：：', answer)
	console.log('[' + userAnswer + ']：：：：：：：：：：：：：：：', userAnswer)
	var answerIndex = 0;
	var userAnswerIndex = 0; // 用户答题下标
	var vernierIndex = 0; // 游尺下标
	var interceptLength = 1; // 截取长度
	function getFtalk(answerStr, userAnswerStr, interceptLength) {
		if (interceptLength > answer.length || vernierIndex > userAnswer.length) {
			// 1.截取的长度超出答案的长度2.下标的长度超出用户答案的长度（正确答案后还有字母）
			console.log('________________________1false')
			return -1
		} else {
			console.log('answerStr', answerStr)
			if (userAnswerStr === '') { // 用户答案和正确答案匹配，但是不相等，则进入等待状态 （答案是apple, 用户拼到appl）
				return 0
			}
			if (answerStr === userAnswerStr) { // 截取的答案===用户的答案
				if (interceptLength === answer.length) {
					// 截取的长度 === 答案的长度
					console.log('________________________1true')
					return 1
				} else {
					if (interceptLength > 1) {
						// 截取的长度>1时，则将截取长度再次置为1，获取下一位字母去判断
						vernierIndex += 1
						answerIndex = interceptLength
						userAnswerIndex += 1
						interceptLength = 1
						return getFtalk(answer.substr(answerIndex, 1), userAnswer.substr(vernierIndex, 1), 1)
					} else {
						vernierIndex += 1
						if ((answerIndex === answer.length - 1) && (userAnswer.length === vernierIndex)) {
							// 1.答案的长度===答案的下标2.用户答案的长度不超过下标的长度
							console.log('________________________2true')
							return 1
						} else {
							answerIndex += 1
							userAnswerIndex += 1
							return getFtalk(answer.substr(answerIndex, 1), userAnswer.substr(vernierIndex, 1), 1)
						}
					}
				}
			} else {
				if (vernierIndex === 0) { // 如果首位拼错则就是错
					console.log('________________________2false')
					return -1
				} else {
					if (userAnswerStr.length > 1) { // 如果不相等并且截取出的长度不为1则错误
						console.log('________________________3false')
						return -1
					} else {
						if (userAnswerStr === answer.substr(0, 1)) { // 如果不相等并且和答案第一位一样则继续向后验证
							// console.log('answerIndex', answerIndex)
							// console.log('answer.length - 1', answer.length - 1)
							// console.log('userAnswer.length', userAnswer.length)
							// console.log('vernierIndex', vernierIndex)
							if (vernierIndex + 1 === userAnswer.length) { // 如果已经是最后一位那么就是错误的
								console.log('________________________4false')
								return 0
							} else {
								interceptLength = 1
								answerIndex = 0
								return getFtalk(answer.substr(answerIndex, 1), userAnswer.substr(vernierIndex, 1), 1)
							}
						} else {
							interceptLength += 1
							var userIndex = userAnswerIndex - 1
							answerIndex = 0
							return getFtalk(answer.substr(answerIndex, interceptLength), userAnswer.substr(userIndex,
								interceptLength), interceptLength)
						}
					}
				}
			}
		}
	}

	var finalStatus = getFtalk(answer.substr(vernierIndex, interceptLength), userAnswer.substr(vernierIndex,
		interceptLength), 1);
	console.log('finalStatus', finalStatus);
	// 调用：
	return finalStatus;
}

function fnSum(a, b) {
	return a + b
}


export default {
	getContentStr,
	getSpellStatus,
	fnSum
}